package com.ruoyi.archives.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.archives.utile.DirectorRoleUtile;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.archives.mapper.SysTeacherArchivesMapper;
import com.ruoyi.archives.domain.SysTeacherArchives;
import com.ruoyi.archives.service.ISysTeacherArchivesService;

/**
 * 老师个人信息Service业务层处理
 * 
 * @author gzhank
 * @date 2022-03-02
 */
@Service
public class SysTeacherArchivesServiceImpl implements ISysTeacherArchivesService 
{
    @Autowired
    private SysTeacherArchivesMapper sysTeacherArchivesMapper;

    @Autowired
    private DirectorRoleUtile directorRoleUtile;

    /**
     * 查询老师个人信息
     * 
     * @param id 老师个人信息主键
     * @return 老师个人信息
     */
    @Override
    public SysTeacherArchives selectSysTeacherArchivesById(String id)
    {
        return sysTeacherArchivesMapper.selectSysTeacherArchivesById(id);
    }

    /**
     * 查询老师个人信息列表
     * 
     * @param sysTeacherArchives 老师个人信息
     * @return 老师个人信息
     */
    @Override
    public List<SysTeacherArchives> selectSysTeacherArchivesList(SysTeacherArchives sysTeacherArchives)
    {
        List<SysTeacherArchives> teacherArchives = sysTeacherArchivesMapper.selectSysTeacherArchivesList(sysTeacherArchives);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        int teDirectorRole = directorRoleUtile.getTEDirectorRole(loginUser);
        if(!SecurityUtils.isAdmin(loginUser.getUserId())&&teDirectorRole!=0){
            ArrayList<SysTeacherArchives> archives = new ArrayList<>();
            for (SysTeacherArchives teacherArchive : teacherArchives) {
                if(teacherArchive.getArcNum().equals(loginUser.getUserId().toString())){
                    archives.add(teacherArchive);
                }
            }
            return archives;
        }else{
            return sysTeacherArchivesMapper.selectSysTeacherArchivesList(sysTeacherArchives);
        }
    }

    /**
     * 新增老师个人信息
     * 
     * @param sysTeacherArchives 老师个人信息
     * @return 结果
     */
    @Override
    public int insertSysTeacherArchives(SysTeacherArchives sysTeacherArchives)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if(sysTeacherArchivesMapper.selectArchivesByUserName(loginUser.getUsername())!=null){
            throw new ServiceException("一人只允许有一份档案！");
        }else{
            sysTeacherArchives.setId(UuidUtils.get8UUID());
            sysTeacherArchives.setArcNum(loginUser.getUserId().toString());
            sysTeacherArchives.setUserName(loginUser.getUsername());
        }
        return sysTeacherArchivesMapper.insertSysTeacherArchives(sysTeacherArchives);
    }

    /**
     * 修改老师个人信息
     * 
     * @param sysTeacherArchives 老师个人信息
     * @return 结果
     */
    @Override
    public int updateSysTeacherArchives(SysTeacherArchives sysTeacherArchives)
    {
        return sysTeacherArchivesMapper.updateSysTeacherArchives(sysTeacherArchives);
    }

    /**
     * 批量删除老师个人信息
     * 
     * @param ids 需要删除的老师个人信息主键
     * @return 结果
     */
    @Override
    public int deleteSysTeacherArchivesByIds(String[] ids)
    {
        return sysTeacherArchivesMapper.deleteSysTeacherArchivesByIds(ids);
    }

    /**
     * 删除老师个人信息信息
     * 
     * @param id 老师个人信息主键
     * @return 结果
     */
    @Override
    public int deleteSysTeacherArchivesById(String id)
    {
        return sysTeacherArchivesMapper.deleteSysTeacherArchivesById(id);
    }

    @Override
    public List<SysTeacherArchives> selectListNumber(SysTeacherArchives sysTeacherArchives) {

        return  sysTeacherArchivesMapper.selectSysTeacherArchivesList(sysTeacherArchives);
    }
}
